// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {
    weather: {}
  },
  // 事件处理函数
  loadWeather() {
    wx.showLoading({
      title: '加载中',
    })

    const background = {
      "大雨": "dayu",
      "中雨": "zhongyu",
      "小雨": "xiaoyu",
      "暴雨": "baoyu",
      "雷阵雨": "leizhenyu",
      "多云": "duoyun",
      "晴": "qing",
      "阴": "yin",
      "阵雪": "zhenxue",
      "中雪": "zhongxue",
      "小雪": "xiaoxue",
      "雨夹雪": "yujiaxue",
      "雾": "wu"
    }

    const week = ['星期天','星期一','星期二','星期三','星期四','星期五','星期六']

    let requestCity = new Promise(function(resolve, reject) {
      wx.request({
        url: 'https://weather.cma.cn/api/map/weather/1?t=1641300733280',
        // url: "http://data.cma.cn/kbweb/home/live",
        // method: "POST",
        data: {
          
        },
        success(res) {
          resolve(res.data)
        },
        fail(err) {
          reject(err)
        },
        complete() {
          wx.hideLoading()
        }
      })
    })

    let weather = {};
    weather.city = "福州"

    requestCity.then((res) => {
      console.log(res)
      if(res.code === 0) {
        let data = res.data;
        let forecast = data.city;
        let futureList = [];

        for(let i=0; i<forecast.length; i++) {
          let one = forecast[i];
          let future = {
            week: week[one[13]],
            type: one[7],
            city: one[2] + "-" + one[1],
            wendu: one[11] + "℃-" + one[6] + "℃"
          }

          futureList.push(future)
        }

        let today = futureList[0];
        if(background[today.type]) {
          today.typeBackground = background[today.type]
        } else {
          today.typeBackground = "default"
        }

        weather.wendu = futureList[0].wendu;
        weather.today = today;
        weather.futureList = futureList;

        this.setData({
          weather: weather
        })

        console.log(weather)
      }
      

    }).catch(err => {
      console.log(err)
    })
  },
  onLoad() {
    this.loadWeather()
  }
})
